package io.renren.common.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.json.JSONObject;


/**
 * 阿里短信工具
 */
public class AliyunSms {

    public static void main(String[] args) {

        AliyunSms.sendCheckCode("LTAI4GBQjbn7XKjfjP9xjKXX", "a9E3OImO2FOo3jEjuFQMi2ZuJHovbD", "15953295779", "金诺会展", "SMS_193510510");

    }

    /**
     * 发送短信验证码
     *
     * @param account
     * @param password
     * @param phones
     * @param signName
     * @param templateCode
     * @return
     */
    public static String sendCheckCode(String account, String password, String phones, String signName, String templateCode) {
        String code = CheckCodeUtils.random() + "";

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", account, password);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("PhoneNumbers", phones);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");

        String result = getCommonResponse(client, request);
        JSONObject res = new JSONObject(result);
        String message = res.getString("Message");
        if("OK".equals(message)){
            return code;
        }else{
            return null;
        }
    }

    /**
     * 发送短信
     *
     * @param account
     * @param password
     * @param phones
     * @param signName
     * @param templateCode 模板内容
     * @param json         模板内容的变量
     * @return
     */
    public static String sendMsg(String account, String password, String phones, String signName, String templateCode, String json) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", account, password);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("PhoneNumbers", phones);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", json);

        return getCommonResponse(client, request);
    }

    public static String getCommonResponse(IAcsClient client, CommonRequest request) {
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }

}
